//
//  main.m
//  5-16复习
//
//  Created by 钟纪旺 on 16/5/16.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Information.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        NSString * path = @"/Users/zhongjiwang/Desktop/cbv.vcf";
        NSError *err;
        NSString *vcf = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
        if (err!=0) {
            NSLog(@"%@",err);
            
        }
        NSArray *arr =[vcf componentsSeparatedByString:@"\n"];
        NSMutableArray *abc =[NSMutableArray new];
        NSMutableDictionary *diti;
        for (NSString *line in arr ) {
            if ([line hasPrefix:@"BEGIN"]) {
                NSLog(@"");
            diti =[NSMutableDictionary new];
            }if ([line hasPrefix:@"END"]) {
                NSLog(@"");
                [abc addObject:diti];
            }if ([line hasPrefix:@"TEL"]) {
                NSArray *tels =[line componentsSeparatedByString:@":"];
                NSString *last =tels.lastObject;
                diti [@"tel"] =last;
            }if([line hasPrefix:@"FN"]){
                
                NSArray *names =[line componentsSeparatedByString:@":"];
                NSString *last =names.lastObject;
                 last = [last stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                last = last.stringByRemovingPercentEncoding;
                diti [@"name"]=last;
                NSLog(@"名字:%@",last);
              }
        }
        
        NSLog(@"%@",abc);
        
        [abc writeToFile:@"/Users/zhongjiwang/Desktop/abc2.txt" atomically:NO];
        NSData *data = [NSJSONSerialization dataWithJSONObject:abc options:NSJSONWritingPrettyPrinted error:nil];
        [data writeToFile:@"/Users/zhongjiwang/Desktop/abc.txt" atomically:NO];
        
        
        NSString *str =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        
        NSMutableArray *res =[NSMutableArray new];
        for (NSDictionary * diti in abc) {
            Information *p =[Information new];
            [p setValuesForKeysWithDictionary:diti];
         //   p.name=@"sadas";
         // setValue:name =@"";
            [p setValue:@"新名字" forKey:@"name"];
            [p setValue:@"110"forKey:@"tel"];
            [res addObject:p];
            NSLog(@"%@",res);
            
}
        
        
        
        
        
        
        
    }
    return 0;
}
